package com.neusoft.yuejian26.yuejian_26.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import retrofit2.Call;

import com.neusoft.yuejian26.yuejian_26.Bean.CheckNamebean;
import com.neusoft.yuejian26.yuejian_26.Bean.Result;
import com.neusoft.yuejian26.yuejian_26.R;
import com.neusoft.yuejian26.yuejian_26.model.UserModel;
import com.neusoft.yuejian26.yuejian_26.network.RetrofitCallback;
import retrofit2.Call;

public class RegActivity extends AppCompatActivity {
    EditText name,tel,upwd,u_confim_pwd;
    Button btn_reg;
    ImageView btn_back;
    String flag;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);
        intview();
        intEvent();
    }

    private void intview() {
        name = (EditText) findViewById(R.id.uname);
        tel = (EditText) findViewById(R.id.utel);
        upwd = (EditText) findViewById(R.id.upwd);
        u_confim_pwd = (EditText) findViewById(R.id.u_confim_pwd);
        btn_reg = (Button) findViewById(R.id.btn_reg);
        btn_back=(ImageView) findViewById(R.id.btn_back);
    }

    private class userreg implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            if(flag.equals("0")) {
                String uname = name.getText().toString().trim();
                final String utel = tel.getText().toString().trim();
                final String pwd = upwd.getText().toString().trim();
                String confim_pwd = u_confim_pwd.getText().toString().trim();
                if (uname.equals("")) {
                    Toast.makeText(RegActivity.this, "请填写用户名", Toast.LENGTH_SHORT).show();
                } else if (utel.equals("")) {
                    Toast.makeText(RegActivity.this, "请填写手机号码", Toast.LENGTH_SHORT).show();
                } else if (!pwd.equals(confim_pwd)) {
                    Toast.makeText(RegActivity.this, "两次密码不一致", Toast.LENGTH_SHORT).show();
                } else {
                    UserModel userModel = new UserModel();
                    Call call = userModel.userRegister(uname, utel, pwd);
                    userModel.bindCallback(call, new RetrofitCallback() {
                        @Override
                        public void onResponse(Object o) {
                            Result result = (Result) o;
                            int state = result.getCode();
                            if (1 == state&&!utel.equals("") && !pwd.equals("")) {
                                Intent intent = new Intent(RegActivity.this, LoginActivity.class);
                                RegActivity.this.startActivity(intent);
                                Toast.makeText(RegActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(RegActivity.this, "请完善信息", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }
            }
        }
    }

    private void intEvent(){
        name.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                String uname = name.getText().toString().trim();
                if (hasFocus) {
                    // 此处为得到焦点时的处理内容
                } else {
                    // 此处为失去焦点时的处理内容
                    UserModel userModel = new UserModel();
                    Call call = userModel.checkName(uname);
                    userModel.bindCallback(call, new RetrofitCallback() {
                        @Override
                        public void onResponse(Object o) {
                            CheckNamebean checkName= (CheckNamebean) o;
                            flag = checkName.getSuccess();
                            if(flag.equals("0")){

                            }
                            else if(flag.equals("1")){
                                Toast.makeText(RegActivity.this,"用户名重复",Toast.LENGTH_SHORT).show();
                                name.setText("");
                            }
                        }
                    });

                }
            }
        });
        btn_reg.setOnClickListener(new userreg());
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

}
